package com.appcenter.application.service;

import com.appcenter.application.dto.permissionrelations.PermissionRelationDto;
import com.appcenter.application.input.permissionrelation.CreatePermissionRelationInput;
import com.appcenter.application.input.permissionrelation.GetPermissionRelationsInput;
import com.appcenter.application.mapper.PermissionRelationsMapper;
import com.appcenter.application.model.PermissionRelations;
import com.appcenter.application.output.permissionrelation.CreatePermissionRelationOutput;
import com.appcenter.application.output.permissionrelation.GetPermissionRelationsOutput;
import com.appcenter.jz.Logger.BusinessLogger;
import com.appcenter.jz.Logger.LoggerGenre;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.unit.ObjectUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import static org.apache.http.util.TextUtils.isBlank;
import static org.apache.logging.log4j.util.Strings.isNotBlank;

@Service
public class PermissionRelationService {
    @Autowired
    PermissionRelationsMapper permissionRelationsMapper;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

//    @CacheEvict(cacheNames="basewarrant", allEntries=true)
//    @Transactional
    /**
     *  创建人:Coke
     *  备注: 创建授权信息
     *  时间: 2019/4/26 14:02 
     *  参数: 
     */
    public CreatePermissionRelationOutput CreatePermissionRelation(CreatePermissionRelationInput input) throws Exception {
        CreatePermissionRelationOutput output = new CreatePermissionRelationOutput();
        ////删除之前的权限
        if (isNotBlank(input.getDataId()) && isNotBlank(input.getAppCode())) {
            Example example = new Example(PermissionRelations.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("dataId", input.getDataId());
            criteria.andEqualTo("appCode", input.getAppCode());
            permissionRelationsMapper.deleteByExample(example);

            //添加新的权限
            List <PermissionRelationDto> permissionRelationDtos = input.getPermissionRelations();
            for (PermissionRelationDto permissionRelationDto : permissionRelationDtos) {
                String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                permissionRelationDto.setId(uuid);
                PermissionRelations permissionRelation = new PermissionRelations();
                BeanUtils.copyProperties(permissionRelationDto, permissionRelation);
                permissionRelationsMapper.insertSelective((permissionRelation));
            }
            BusinessLogger.Info(LoggerGenre.Add, "添加授权信息", input);
            output.setPermissionRelations(permissionRelationDtos);
        }
        return output;
    }

//    @Transactional
    /**
     *  创建人:Coke
     *  备注: 获取授权信息
     *  时间: 2019/4/26 14:13
     *  参数: 
     */
    public GetPermissionRelationsOutput GetPermissionRelations(GetPermissionRelationsInput input) throws Exception {
        GetPermissionRelationsOutput output = new GetPermissionRelationsOutput();
        List <PermissionRelationDto> ls = permissionRelationsMapper.getPermissionRelationByDataId(input);
        BusinessLogger.Info(LoggerGenre.View, "获取授权信息", input);
        output.setPermissionRelations(ls);
        return output;
    }


}
